package com.training.controller;

import cn.dev33.satoken.util.SaResult;
import com.baomidou.mybatisplus.extension.toolkit.Db;
import com.training.dto.ConfirmAqiDataDto;
import com.training.entity.Address;
import com.training.entity.ConfirmAqiData;
import com.training.entity.PublicSurveillanceData;
import com.training.entity.User;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.Objects;


@RestController
@RequestMapping("/confirmAqiData")
@Tag(name = "确认aqi数据管理")
public class ConfirmAqiController {

    @Operation(summary = "获取确认aqi数据")
    @GetMapping
    public SaResult getConfirmAqiData(@RequestParam(defaultValue = "1")Integer page,
                                      @RequestParam(defaultValue = "10")Integer pageSize,
                                      @RequestParam(defaultValue = "")String province,
                                      @RequestParam(defaultValue = "") String city,
                                      LocalDateTime startDate, LocalDateTime endDate) {
        List<ConfirmAqiData> confirmAqiData;
        if (Objects.isNull(startDate) || Objects.isNull(endDate)) {
            confirmAqiData = Db.lambdaQuery(ConfirmAqiData.class).list();
        } else {
            confirmAqiData = Db.lambdaQuery(ConfirmAqiData.class)
                    .between(ConfirmAqiData::getUpdateTime, startDate, endDate)
                    .list();
        }
        confirmAqiData = confirmAqiData.stream().filter(e -> {
            Address address = Db.lambdaQuery(Address.class).eq(Address::getId, e.getAddressId())
                    .like(Address::getProvince, province).like(Address::getCity, city).one();
            return Objects.nonNull(address);
        }).toList();
        int start = (page - 1) * pageSize;
        int end = Math.min(page * pageSize, confirmAqiData.size());
        int total = confirmAqiData.size();
        List<ConfirmAqiData> confirmAqiDataList = confirmAqiData.subList(start, end);
        List<ConfirmAqiDataDto> confirmAqiDataDtos = confirmAqiDataList.stream().map(e -> {
            PublicSurveillanceData surveillanceData = Db.getById(e.getSurveillanceId(), PublicSurveillanceData.class);
            User inspector = Db.getById(e.getInspectorId(), User.class);
            User supervisor = Db.getById(surveillanceData.getSupervisorId(), User.class);
            Address address = Db.getById(e.getAddressId(), Address.class);
            return ConfirmAqiDataDto.builder()
                    .id(e.getId())
                    .aqi(e.getAqi())
                    .supervisor(supervisor)
                    .inspector(inspector)
                    .description(surveillanceData.getDescription())
                    .isConfirmed(e.getIsConfirmed())
                    .confirmAddress(address)
                    .updateTime(e.getUpdateTime())
                    .build();
        }).toList();
        Map<String , Object> map = Map.of("total",total, "data", confirmAqiDataDtos);
        return SaResult.data(map);
    }


    @Operation(summary = "获取确认aqi数据")
    @GetMapping("/list")
    public SaResult getConfirmAqiDataList(@RequestParam(defaultValue = "1")Integer page,
                                      @RequestParam(defaultValue = "10")Integer pageSize,
                                      @RequestParam(defaultValue = "")String province,
                                      @RequestParam(defaultValue = "") String city,
                                      LocalDateTime startDate, LocalDateTime endDate) {
        List<ConfirmAqiData> confirmAqiData;
        if (Objects.isNull(startDate) || Objects.isNull(endDate)) {
            confirmAqiData = Db.lambdaQuery(ConfirmAqiData.class).list();
        } else {
            confirmAqiData = Db.lambdaQuery(ConfirmAqiData.class)
                    .between(ConfirmAqiData::getUpdateTime, startDate, endDate)
                    .list();
        }
        confirmAqiData = confirmAqiData.stream().filter(e -> {
            Address address = Db.lambdaQuery(Address.class).eq(Address::getId, e.getAddressId())
                    .like(Address::getProvince, province).like(Address::getCity, city).one();
            return Objects.nonNull(address);
        }).toList();
        int start = (page - 1) * pageSize;
        int end = Math.min(page * pageSize, confirmAqiData.size());
        List<ConfirmAqiData> confirmAqiDataList = confirmAqiData.subList(start, end);
        List<ConfirmAqiDataDto> confirmAqiDataDtos = confirmAqiDataList.stream().map(e -> {
            PublicSurveillanceData surveillanceData = Db.getById(e.getSurveillanceId(), PublicSurveillanceData.class);
            User inspector = Db.getById(e.getInspectorId(), User.class);
            User supervisor = Db.getById(surveillanceData.getSupervisorId(), User.class);
            Address address = Db.getById(e.getAddressId(), Address.class);
            return ConfirmAqiDataDto.builder()
                    .id(e.getId())
                    .aqi(e.getAqi())
                    .supervisor(supervisor)
                    .inspector(inspector)
                    .description(surveillanceData.getDescription())
                    .isConfirmed(e.getIsConfirmed())
                    .confirmAddress(address)
                    .updateTime(e.getUpdateTime())
                    .build();
        }).toList();
        Map<String , Object> map = Map.of("total", confirmAqiData.size(), "data", confirmAqiDataDtos);
        return SaResult.data(map);
    }

    @Operation(summary = "根据id获取确认aqi数据")
    @GetMapping("/{id}")
    public SaResult getConfirmAqiDataById(@PathVariable String id) {
        ConfirmAqiData confirmAqiData = Db.getById(id, ConfirmAqiData.class);
        PublicSurveillanceData surveillanceData = Db.getById(confirmAqiData.getSurveillanceId(), PublicSurveillanceData.class);
        User supervisor = Db.getById(surveillanceData.getSupervisorId(), User.class);
        User inspector = Db.getById(confirmAqiData.getInspectorId(), User.class);
        Address address = Db.getById(confirmAqiData.getAddressId(), Address.class);
        ConfirmAqiDataDto confirmAqiDataDto = ConfirmAqiDataDto.builder()
                .id(confirmAqiData.getId())
                .aqi(confirmAqiData.getAqi())
                .supervisor(supervisor)
                .inspector(inspector)
                .description(surveillanceData.getDescription())
                .isConfirmed(confirmAqiData.getIsConfirmed())
                .confirmAddress(address)
                .updateTime(confirmAqiData.getUpdateTime())
                .build();
        return SaResult.data(confirmAqiDataDto);
    }
}
